package cn.com.lyb.config;

import cn.com.lyb.utils.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RedissionConfig {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port;

    @Value("${spring.redis.database}")
    private int database;

    @Value("${spring.redis.password}")
    private String password;


    @Bean
    public RedissonClient getRedisson() {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://" + host + ":" + port)
                .setDatabase(database);
        if (!StringUtils.isEmpty(password)) {
            config.useSingleServer().setAddress("redis://" + host + ":" + port).setDatabase(database)
                    .setPassword(password);
        } else {
            config.useSingleServer().setAddress("redis://" + host + ":" + port).setDatabase(database);
        }
        //设置全局默认看门狗机续期时间,如果在使用时不设置，则使用全局的，如果全局不设置，则使用默认的30000,单位毫秒
        config.setLockWatchdogTimeout(2000);
        return Redisson.create(config);
    }
}
